let v = new Vue({
    el:'#app',
    data:{
        user:{
            email:'',
            username:'',
            password:''
        },
        code:'',
        msg:' ',
        checkUsernameMsg:" ",
        checkPawMsg:' '
    },
    methods:{
        //检查邮箱格式
        checkEmail:function () {
            let regEmail = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
            if (!regEmail.test(this.user.email)){
                this.msg="邮箱格式不正确，请重新输入";
            }else{
                axios({
                    url:`checkEmail/${this.user.email}`
                }).then(response=>{
                    if (!response.data.flag){
                        this.msg="";
                    }else{
                        this.msg="该邮箱不存在，请重新输入";
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                });
            }
        },
        checkUserName:function (){
            let regName = /(^[a-zA-Z0-9_-]{4,16}$)|(^[\u2E80-\u9FFF]{2,8})/;
            if (!regName.test(this.user.username)){
                this.checkUsernameMsg="用户名不规范，用户名可以是2-8位中文或者4-16位英文和数字的组合";
            }else{
                axios({
                    url:`checkUserName/${this.user.username}`
                }).then(response=>{
                    if (!response.data.flag){
                        this.checkUsernameMsg="";
                    }else{
                        this.checkUsernameMsg="用户名不存在，请重新输入";
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                })
            }
        },
        checkPassword:function(){
            if (this.user.password ==="" || this.user.password.length<4 ){
                this.checkPawMsg = "密码不符合规范，请输入4位以上密码"
            }else {
                this.checkPawMsg = "";
            }
        },
        //发送验证码
        sendCode:function(){
            if (this.msg !== ''){
                layer.msg("请填写正确的邮箱格式");
                return false;
            }
            axios.put(`sendCode/${this.user.email}`).then(rsp=>{
                if (rsp.data.flag){
                    layer.msg("发送成功")
                }
            })
        },
        //重置密码
        resetting:function () {
            if (this.msg !== '' && this.checkPawMsg !=='' && this.checkUsernameMsg !==''){
                layer.msg("信息填写有误，请检查");
                return false;
            }else if(this.code === ''){
                layer.msg("验证码为空");
                return false;
            }
            axios.put(`updatePassword/${this.code}`,this.user)
                .then(resp=>{
                    if (resp.data.flag){
                        layer.msg("修改成功,3秒后跳转到登录页面");
                        // 删除cookie
                        $.removeCookie("userId", {path: '/'});
                        //删除session
                         localStorage.removeItem("userId");
                         localStorage.removeItem("username");
                        setTimeout(()=>{
                            location.href='/toLogin' },3000);
                    }else{
                        layer.msg(resp.data.data);
                        this.user.password = "";
                        this.code = "";
                    }
                })
        }
    }
});